package pl.puntin.punclient.view.game;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

public final class DrawPadComponent extends JPanel
{

	private static final int IMAGE_WIDHT = 640;
	private static final int IMAGE_HEIGHT = 480;
	
	private BufferedImage bufferedImage;
	private final Graphics2D graphics2D;
	private int currentX, currentY, oldX, oldY;

	public DrawPadComponent()
	{
		bufferedImage = new BufferedImage(IMAGE_WIDHT, IMAGE_HEIGHT, BufferedImage.TYPE_BYTE_BINARY);
		graphics2D = bufferedImage.createGraphics();
		
		setPreferredSize(new Dimension(IMAGE_WIDHT, IMAGE_HEIGHT));

		
		addMouseListener(new MouseAdapter()
		{
			@Override
			public void mousePressed(final MouseEvent e)
			{
				oldX = e.getX();
				oldY = e.getY();
			}
		});

		addMouseMotionListener(new MouseMotionAdapter()
		{
			@Override
			public void mouseDragged(final MouseEvent e)
			{
				currentX = e.getX();
				currentY = e.getY();

				if (graphics2D != null)
					graphics2D.drawLine(oldX, oldY, currentX, currentY);
				
				repaint();

				oldX = currentX;
				oldY = currentY;
			}
		});
		
//		DataBuffer dataBuffer = getBufferedImage().getData().getDataBuffer();
//		System.out.println("typ "+dataBuffer.getDataType());
//		System.out.println("banki " + dataBuffer.getNumBanks());
//		System.out.println("rozmiar " + dataBuffer.getSize());
//		System.out.println("rozmiarTypu " + (dataBuffer.getDataType() == DataBuffer.TYPE_BYTE));
	}
	
	@Override
	protected void paintComponent(final Graphics graphics)
	{
		graphics.drawImage(bufferedImage, 0, 0, null);
	}

	public BufferedImage getBufferedImage()
	{
		return bufferedImage;
	}
	
	public void setBufferedImage(final BufferedImage bufferedImage)
	{
		this.bufferedImage = bufferedImage;
	}
}
